"""
数据模型层
"""
from db.dbhandler import save_data, read_data, is_file_exist, get_all


class User:
    """系统用户基类"""

    def __init__(self, name: str, password: str, age: int, gender: str):
        self.name = name  # 姓名
        self.password = password  # 密码
        self.age = age  # 年龄
        self.gender = gender  # 性别 男 女


class FileMixin:
    """文件操作Mixin类"""

    def save(self):
        """保存数据"""
        save_data(self)

    @classmethod
    def read(cls, obj_name: str):
        """读取数据"""
        return read_data(name=obj_name, class_name=cls.__name__)

    @classmethod
    def is_exist(cls, obj_name) -> bool:
        """对象是否已存在"""
        return is_file_exist(name=obj_name, class_name=cls.__name__)

    @classmethod
    def all_file_list(cls) -> list:
        """返回所有列表"""
        return get_all(class_name=cls.__name__)

    def __eq__(self, other):
        return self.name == other.name


class School(FileMixin):
    """学校类"""

    def __init__(self, school_name, address):
        self.name = school_name  # 学校名称
        self.address = address  # 学校地址
        self.mail = f'{school_name}@sh.com'  # 邮箱
        self.course_list = []  # 课程列表


class Course(FileMixin):
    """课程类"""

    def __init__(self, course_name, price):
        self.name = course_name  # 课程名
        self.price = price  # 价格
        self.student_list = []  # 课程学生列表


class Admin(FileMixin, User):
    """管理员类"""

    def __init__(self, name: str, password: str):
        super().__init__(name, password, age=0, gender='')  # 默认管理员年龄为0,性别为空


class Student(FileMixin, User):
    """学生类"""

    def __init__(self, name: str, password: str, age: int, gender: str):
        super().__init__(name, password, age, gender)
        self.school_name = ''
        self.score_dict = {}  # 课程字典：课程名:分数


class Teacher(FileMixin, User):
    """教师类"""

    def __init__(self, name: str, password: str, age: int, gender: str):
        super().__init__(name, password, age, gender)
        self.course_name_list = []  # 课程列表
